<?php

namespace Api\Event;

/**
 * 站内信接口
 * @author Legend. <xcx_legender@qq.com>
 * @param
 * @return
 */

class MailEvent extends BaseEvent {
	// 是否有新消息
	public function isNew($uid) {
		$map = ['target_id' => (int) $uid, 'is_read' => 0];
		return service('MailUsers')->where($map)->count() > 0;
	}

	// 获取对话列表
	public function lists($uid, $target_id, $timestamp = 0) {
		$uid = (int) $uid;
		$target_id = (int) $target_id;
		// $map = [
		// 	'uid' 		=> $uid,
		// 	'target_id' => $target_id
		// ];

		$query['$and'][]['$or'] = [
			[
				'uid' => $uid,
				'target_id' => $target_id,
			],
			[
				'uid' => $target_id,
				'target_id' => $uid,
			],
		];
		$query['$and'][]['send_time'] = $timestamp ? ['$lt' => $timestamp] : ['$gt' => 0];
		$map['_array'] = $query;
		$list = service('Mail')->where($map)->order('send_time desc')->limit(5)->select();
		$this->setRead($uid, $target_id);
		return $list;
	}

	// 获取最近的朋友
	public function getFriendsList($uid, $p = 1, $pager = 10) {
		$p = $p > 0 ? $p : 1;
		$offset = ($p - 1) * $pager;
		$map['target_id'] = (int) $uid;
		$list = service('MailUsers')->where($map)->order('send_time desc')->limit("$offset,$pager")->select();
		$list = field_filter($list, ['uid', 'send_time', 'content', 'is_read'], true);
		return $list;
	}

	/**
	 * [给某人发一条信息]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	public function send($uid, $target_id, $content) {
		# 插入到msg表内
		$data = [
			'uid' => (int) $uid,
			'target_id' => (int) $target_id,
			'content' => $content,
		];
		$serv = service('Mail');
		$data = $serv->create($data);
		$res = $data && $serv->add($data);
		if (!$res) {
			return false;
		}

		# 更新一条信息提示表
		$this->setMailObj($uid, $target_id, $content);
		// 给自己发一条
		$this->setMailObj($target_id, $uid, $content);
		# 发送推送
		// $user = get_nickname($uid);
		// $nickname = get_nickname($target_id);
		$sender = [
			'uid' => $uid,
			'nickname' => get_nickname($uid),
		];
		$to = [
			'uid' => $target_id,
			'nickname' => get_nickname($target_id),
		];

		event('Push')->mail($sender, $to, $content);
		return true;
	}

	/**
	 * [设置对此人的一条私信信息 参考 Common\Service\MailUsersService 作为信息提示]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	protected function setMailObj($uid, $target_id, $content) {
		$map['uid'] = (int) $uid;
		$map['target_id'] = (int) $target_id;
		$serv = service('MailUsers');
		$mail = $serv->where($map)->find();
		$data['is_read'] = 0;
		$data['send_time'] = NOW_TIME;
		$data['read_time'] = 0;
		$data['content'] = htmlspecialchars($content);
		if (!$mail) {
			$data = array_merge($data, $map);
			$res = $serv->add($data);
		} else {
			// \Think\Log::record("DEBUG: API: data".http_build_query($data), 'DEBUG');
			$res = $serv->where($map)->save($data);
		}
		return $res;
	}

	/**
	 * [设置已读]
	 * @author Legend. <xcx_legender@qq.com>
	 * @param
	 * @return
	 */
	public function setRead($uid, $target_id) {
		$map = ['uid' => (int) $target_id, 'target_id' => (int) UID];
		$serv = service('MailUsers');
		$save = ['is_read' => 1, 'read_time' => NOW_TIME];
		$serv->where($map)->save($save);
	}

}